from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
import dbutil
import pymainwindow

ed, _ = loadUiType('editmemmation.ui')

class editmemmationApp(QMainWindow,ed):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.confirmeditButton.clicked.connect(self.edit_member2)
        self.menu.triggered[QAction].connect(self.menuTriggered)

    def menuTriggered(self, q):
        if (q.text() == "返回上一页"):
            self.mainwindow_app = pymainwindow.MWApp()
            self.close()
            self.mainwindow_app.show()

        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return

    def edit_member2(self):
        num = self.numEdit.text()
        name = self.nameEdit.text()
        sex = self.sexEdit.text()
        category = self.categoryEdit.text()
        phone = self.phoneEdit.text()
        # 获取连接
        conn = dbutil.get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql = "update memberinformation set 姓名=%s,性别=%s,会员类别=%s,联系电话=%s where 会员编号=%s"
        if (num=='' or name=='' or sex=='' or category=='' or phone=='' ):
            QMessageBox.warning(self,"警告", "不能为空！", QMessageBox.Yes)
        else:
            warning = QMessageBox.warning(self, "提示", "您确定修改此条信息吗？", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                # 执行语句
                cur.execute(sql, (name, sex, category, phone, num))
                # insert,update,delete必须显示提交
                conn.commit()
                # 关闭资源
                dbutil.close_conn(conn, cur)
                # 消息提示
                QMessageBox.information(self, "提示", "修改成功!",QMessageBox.Yes)
                self.numEdit.setText('')
                self.nameEdit.setText('')
                self.sexEdit.setText('')
                self.categoryEdit.setText('')
                self.phoneEdit.setText('')


def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = editmemmationApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication


if __name__ == '__main__':
    main()
